package me.magicall.test.backup;

import java.awt.Color;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.jmge.gif.Gif89Encoder;

/**
 * @author Huang Jing
 */
public class RandImageServlet extends HttpServlet {
	private static final long serialVersionUID = -6991800819172168727L;
	private static final Random RANDOM = new Random();
	private static final String MOL_RANDOM = "MOL_RANDOM_TOKEN";

	//因为主工程去session运动，但又担心其他应用需要使用session,所以增加了这个开关值
	private boolean needSession = true;

	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
		final String postfix = request.getParameter("post");

		response.setContentType("image/gif");

		final BufferedImage bi = new BufferedImage(42, 16, BufferedImage.TYPE_3BYTE_BGR);
		bi.getGraphics().setFont(new Font("Arial", Font.BOLD, 20));
		bi.getGraphics().setColor(Color.WHITE);

		final String rand = String.valueOf(RANDOM.nextInt(10)) + RANDOM.nextInt(10) + RANDOM.nextInt(10) + RANDOM.nextInt(10);

		bi.getGraphics().drawString(rand, 4, 12);
		final Gif89Encoder enc = new Gif89Encoder(bi);

		if (needSession) {
			final HttpSession session = request.getSession(true);
			session.setAttribute(MOL_RANDOM + postfix, rand);
		}
//		MemCacheManager mcm = MemCacheFactory.getInstance().getManagerByPoolName(MemCacheKeys.pool_icode);
//		String cacheRand = (String)mcm.get(MOL_RANDOM + postfix);
//		if(cacheRand!=null&&!cacheRand.equals("")){
//			mcm.set(MOL_RANDOM + postfix,rand,new Date(60*30*1000));
//		}else
//			mcm.add(MOL_RANDOM + postfix,rand,new Date(60*30*1000));

		enc.encode(response.getOutputStream());
	}

	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(final HttpServletRequest arg0, final HttpServletResponse arg1) throws ServletException, IOException {
		doGet(arg0, arg1);
	}

	/*
	 * (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void service(final HttpServletRequest arg0, final HttpServletResponse arg1) throws ServletException, IOException {
		doGet(arg0, arg1);
	}

	@Override
	public void init(final ServletConfig config) throws ServletException {
		super.init(config);
		final String ns = config.getInitParameter("needSession");
		if ("0".equals(ns) || "false".equals(ns)) {
			needSession = false;
		}
	}

}
